package br.com.dispositivosmoveis.acompanhamentodeconta.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import br.com.dispositivosmoveis.acompanhamentodeconta.R;
import br.com.dispositivosmoveis.acompanhamentodeconta.abstracts.AbstractActivity;
import br.com.dispositivosmoveis.acompanhamentodeconta.abstracts.NumeroComandaService;
import br.com.dispositivosmoveis.acompanhamentodeconta.model.Comanda;
import br.com.dispositivosmoveis.acompanhamentodeconta.model.NumeroComanda;
import br.com.dispositivosmoveis.acompanhamentodeconta.service.ComandaServiceImpl;
import br.com.dispositivosmoveis.acompanhamentodeconta.service.NumeroComandaServiceImpl;

public class InserirComandaActivity extends
		AbstractActivity<Comanda, ComandaServiceImpl> {

	private Button btnInserir;
	private EditText edtNumeroComanda;
	private NumeroComandaService numeroComandaService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inserir_comanda);

		this.btnInserir = (Button) findViewById(R.id.btnInserir);
		this.edtNumeroComanda = (EditText) findViewById(R.id.edtNumeroComanda);

		this.startEvents();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.inserir_comanda, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void startEvents() {
		this.btnInserir.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				persit();
			}
		});
	}

	@Override
	public void persit() {
		this.getInstance().setNumeroComanda(new NumeroComanda());
		this.getInstance().getNumeroComanda()
				.setNumero((edtNumeroComanda.getText().toString()));
		if (formValidate(getInstance())) {
			super.persit();
			Intent intent = new Intent(InserirComandaActivity.this,
					ConsultarItemActivity.class);

			this.startActivity(intent);
		}
	}

	@Override
	public boolean formValidate(Comanda entity) {
		numeroComandaService = new NumeroComandaServiceImpl();
		boolean validated = true;
		StringBuilder msg = new StringBuilder();

		if (entity.getNumeroComanda().getNumero() == null
				|| "".equals(entity.getNumeroComanda().getNumero())) {
			validated = false;
			msg.append("");
		} else if (!numeroComandaService.checkStatus(entity.getNumeroComanda()
				.getNumero())) {
			validated = false;
			msg.append("");
		}

		return validated;
	}
}
